//
//  PasswordManageViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/16.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PasswordManageViewController: BaseTableViewController {
    
    override var names: Array<String>{
        return ["modify"]
    }
    var iflogin = false
    var phone = ""
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if let result = json.dictionary?["success"]?.rawValue as? Bool {
                if result == true {
                    self.navigationController?.popToRootViewController(animated: true)
                } else {
                    self.alert(json.dictionary?["errorMsg"]?.rawString())
                }
            }
        }
        return suc
    }
    override func param(for name: String) -> Dictionary<String, Any> {
        if self.iflogin {
            return ["account": self.phone,"oldPassword":self.oldPassField.text!,"password":self.newPassField.text!,"confirmPwd":self.confirmPassField.text!]
        }
        return ["oldPassword":self.oldPassField.text!,"password":self.newPassField.text!,"confirmPwd":self.confirmPassField.text!]
        
    }
    
    override func path(for name: String) -> String {
        if self.iflogin {
            return "basicComponents/mobile/sys/authority/updateLoginPwd"
        }
        return "basicComponents/mobile/sys/authority/updateUserPwd"
    }
    
    override func load(with name: String) -> Void {
        //得找一个有效策略
        let result = strategy(for: name)
        
        if result.0 == false {
            if result.2 != nil {
                result.2!();
            }else{
                self.alert(result.1)
            }
            return
        }
        self.beginLoading()
        switch self.method(for: name) {
        case .get:
            NetManager.get(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
            break
        default:
            NetManager.post(self.path(for: name), self.param(for: name), suc: {(source) in
                self.endLoading()
                self.success(for: name)(source)
            }, fail: {(error) in
                self.endLoading()
                self.failBlock(for: name)(error)
            })
        }
    }
    
    override func method(for name: String) -> NetMethod {
        return .post
    }
    
    override func strategy(for name: String) -> (Bool, String?, (() -> Void)?) {
        
        self.view.endEditing(true)
        
        if self.oldPassField.text == nil {
            
            return (false,"请输入原始密码",nil)
            
        }
        
        if self.newPassField.text == nil {
            
            return (false,"请输入新密码",nil)
            
        }
        
        if self.confirmPassField.text == nil {
            
            return (false,"请输入确认密码",nil)
            
        }
        
        if self.oldPassField.text!.count < 6 || self.oldPassField.text!.count > 20 {
            
            return (false,"请输入6～20位旧密码",{() in
                
                self.oldPassField.text = ""
                
                self.oldPassField.becomeFirstResponder()
                
                self.alert("请输入6~20位旧密码")
            })
            
        }
        
        if self.newPassField.text!.count < 6 || self.newPassField.text!.count > 20 {
            
            return (false,"请输入6～20位新密码",{() in
                
                self.newPassField.text = ""
                
                self.newPassField.becomeFirstResponder()
                
                self.alert("请输入6~20位新密码")
            })
            
        }
        
        if confirmPassField.text != self.newPassField.text {
            return (false,"确认密码不一致",{() in
                self.confirmPassField.text = ""
                self.confirmPassField.becomeFirstResponder()
                self.alert("确认密码不一致")
            })
        }
        
        return (true,nil,nil)
        
    }
    
    @IBOutlet weak var oldPassField: UITextField!
    
    @IBOutlet weak var newPassField: UITextField!
    
    @IBOutlet weak var confirmPassField: UITextField!
    
    @IBAction func confirmAction(_ sender: UIButton) {
        self.load(with: "modify")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        let back = UIBarButtonItem.init(image: UIImage(named: "icon_nav_back")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(popobjc))
        self.navigationItem.leftBarButtonItem = back
        
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10.0
    }
}
